﻿using System;
using System.Collections.Generic;
using System.Text;
using LantinxShare.Errors.LBS;
using LantinxShare.Common;

namespace LantinxShare.LBS.Location
{
    /// <summary>
    /// 位置对象
    /// </summary>
    public class GLocation
    {
        public GPoint Location { get; set; }
        public String Title { get; set; }
        public DateTime GTimeStamp { get; set; }
        /// <summary>
        /// 速度(m/s)
        /// </summary>
        public double Speed { get; set; }

        /// <summary>
        /// 海拔
        /// </summary>
        public double Altitude { get; set; }

        /// <summary>
        /// 数据格式: Title,精度,纬度,时间,速度,海拔 用'|'分割 时间秒为00
        /// </summary>
        /// <returns>数据格式: Title,精度,纬度,时间,速度,海拔 用'|'分割</returns>
        public override string ToString()
        {
            return Title + G._ + Location.Longitude + G._ + Location.Latitude + G._ + GTimeStamp.ToString(G.DD) + G._ + Speed + G._ + Altitude;
        }



        #region 辅助方法

        /// <summary>
        /// 根据位置集合取得字符串
        /// </summary>
        /// <param name="locList">位置集合</param>
        /// <returns>返回字符串 数据格式: Title,精度,纬度,时间,速度,海拔 用'|'分割</returns>
        public static string GetGLocationString(List<GLocation> list)
        {
            //StringBuilder builder = new StringBuilder();
            //if (locList != null)
            //{
            //    foreach (var item in locList)
            //    {
            //        builder.Append(item.ToString());
            //        builder.Append(G.P);
            //    }
            //}
            StringBuilder builder = new StringBuilder();
            if (list.Count > 0)
            {
                builder.Append(list[0]);
                for (int i = 1; i < list.Count; i++)
                {

                    builder.Append(G.P + list[i]);
                }
            }
            return builder.ToString();
        }

        /// <summary>
        /// 将新的位置与原位置集合比较，并将新增的位置加入原位置集合。比较两个位置的经纬度，还有海拔，其它参数不参与比较。
        /// </summary>
        /// <param name="sourceLocList">原位置集合</param>
        /// <param name="newLocList">新位置集合</param>
        public static List<GLocation> CompareAdd(List<GLocation> sourceLocList, List<GLocation> newLocList)
        {
            List<GLocation> newList = new List<GLocation>();
            if (sourceLocList == null || newLocList == null)
            {
                return  newList;
            }
           
            foreach (var item in newLocList)
            {
                bool isContain = false;
                foreach (var eachAddress in sourceLocList)
                {
                    if (eachAddress.Location.Latitude == item.Location.Latitude
                        && eachAddress.Location.Longitude == item.Location.Longitude && eachAddress.GTimeStamp.ToString(G.DD) == item.GTimeStamp.ToString(G.DD))
                    {
                        isContain = true;
                        break;
                    }
                }
                if (!isContain)
                {
                    sourceLocList.Add(item);
                    newList.Add(item);
                }
                else { }
            }
            return newList;
        }

        /// <summary>
        ///  将String字符串转换为位置对象 数据格式: Title,精度,纬度,时间,速度,海拔 用'|'分割
        /// </summary>
        /// <param name="result">位置字符串</param>
        /// <returns>位置集合</returns>
        public static List<GLocation> GetGLocations(string result)
        {
            List<GLocation> list = new List<GLocation>();

            if (!string.IsNullOrEmpty(result))
            {
                try
                {
                    List<String[]> locList = G.GetSplit(result);
                    foreach (var item in locList)
                    {
                        string c = item[3];
                        DateTime date = new DateTime(
                                            int.Parse(c.Substring(0, 4)),
                                            int.Parse(c.Substring(4, 2)),
                                            int.Parse(c.Substring(6, 2)),
                                            int.Parse(c.Substring(8, 2)),
                                            int.Parse(c.Substring(10, 2)), 0);
                        list.Add(new GLocation()
                        {
                            Title = item[0],
                            Location = new GPoint(double.Parse(item[1]),double.Parse(item[2])),
                            GTimeStamp = date,
                            Speed = double.Parse(item[4]),
                            Altitude = double.Parse(item[5]),
                        });
                    }

                    //string[] addressArray = result.Split('|');
                    //if (addressArray != null)
                    //{
                    //    foreach (var item in addressArray)
                    //    {
                    //        string[] addressGroup = item.Split(',');
                    //        if (addressGroup != null && addressGroup.Length >= 2)
                    //        {
                    //            DateTime cDateStamp = DateTime.Now;
                    //            if (addressGroup.Length > 3)
                    //            {
                    //                String c = addressGroup[3];
                    //                try
                    //                {
                    //                    cDateStamp = new DateTime(
                    //                        int.Parse(c.Substring(0, 4)),
                    //                        int.Parse(c.Substring(4, 2)),
                    //                        int.Parse(c.Substring(6, 2)),
                    //                        int.Parse(c.Substring(8, 2)),
                    //                        int.Parse(c.Substring(10, 2)), 0);
                    //                }
                    //                catch
                    //                {
                    //                    throw new GLBSDateException(c);
                    //                }
                    //            }
                    //            double speed = 0d;
                    //            if (addressGroup.Length > 4)
                    //            {
                    //                speed = double.Parse(addressGroup[4]);
                    //            }
                    //            double altitude = 0d;
                    //            if (addressGroup.Length > 5)
                    //            {
                    //                altitude = double.Parse(addressGroup[5]);
                    //            }
                    //            list.Add(new GLocation()
                    //            {
                    //                Location = new GPoint(double.Parse(addressGroup[1]),
                    //                    double.Parse(addressGroup[2])),
                    //                Title = addressGroup[0],
                    //                GTimeStamp = cDateStamp,
                    //                Speed = speed,
                    //                Altitude = altitude
                    //            });

                    //        }
                    //    }

                    //}
                }
                catch
                {
                    throw new GLBSStringToLocation(result);
                }
            }
            return list;
        }

        #endregion
    }// end for class

}